package com.htwy.core.controller;

import com.htwy.core.model.Dataset;
import com.htwy.ext.BaseController;

import com.jfinal.kit.Kv;
import com.jfinal.kit.Okv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;

public class DatasetController extends BaseController<Dataset> {

    @Override
    protected Dataset dao() {
        return Dataset.dao;
    }


    public void findByCodes(){
        String[] codes = getParaValues("codes");
        if(codes.length>0) {
            Kv data = Kv.create();
            for (String code : codes) {
                List<Record> records = dao().getDataByCode(code);
                if(records != null && records.size()>0){
                    Okv okv = Okv.create();
                    for (Record temp : records) {
                        okv.set(temp.getStr("code"), temp.getStr("label"));
                    }
                    data.set(code, okv);
                }
            }
            renderJson(Ret.ok().set("data",data));
        }else {
            renderJson(Ret.fail().set("msg","至少需要一个数据源"));
        }
    }

}